package com.fs.console.base.config;

import com.fs.console.base.interceptor.AuthenticationInterceptor;
import com.fs.console.base.vo.SysPath;
import lombok.Getter;
import lombok.Setter;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * @author ACE
 * @desc 数据拦截器
 * @creationTime 2019-03-07 12:00
 * @projectName gps-root
 * @packageName com.fs.base.config
 */
@Configuration
public class InterceptorConfig implements WebMvcConfigurer{
    @Setter @Getter @Value("${sysconfig.backExcludePath}")
    private String backExcludePath;
    @Setter @Getter @Value("${sysconfig.actuatorPath}")
    private String actuatorPath;
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        /*
         * 免拦截路径
         */
        String[] excludePath = {};
        String[] actuatorPathArr = {};
        if(StringUtils.isNotBlank(backExcludePath)){ excludePath = backExcludePath.split(","); }
        if(StringUtils.isNotBlank(actuatorPath)){ actuatorPathArr = actuatorPath.split(","); }

        registry.addInterceptor(authenticationInterceptor())
                .addPathPatterns("/**")
                .excludePathPatterns(SysPath.BACK_LOGIN_PATH)
                .excludePathPatterns(SysPath.SWAGGER_PNG)
                .excludePathPatterns(SysPath.SWAGGER_RESOURCES)
                .excludePathPatterns(SysPath.SWAGGER_UI)
                .excludePathPatterns(excludePath)
                .excludePathPatterns(actuatorPathArr);    // 拦截所有请求，通过判断是否有 @LoginRequired 注解 决定是否需要登录
    }

    /**
     * 全局配置跨域可访问资源
     * @param registry
     */
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**");
    }

    @Bean
    public AuthenticationInterceptor authenticationInterceptor() {
        return new AuthenticationInterceptor();
    }
}